﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SaleOfMoviesSystem.Xml.Models;
using System.Xml;
using SaleOfMoviesSystem.Xml.Enum;

namespace SaleOfMoviesSystem.Xml.Services
{
    public class MovieServices
    {
        public List<Movie> Movies { get; set; } = new List<Movie>();

        public MovieServices()
        {
            //加载 xml 文件 -- 反序列化操作 , xpath
            var doc = new XmlDocument();
            doc.Load(@"Data\Movies.xml");
            var moviesNode = doc.SelectNodes("Movies/Movie");
            foreach (XmlNode movieNode in moviesNode)
            {
                var movie = new Movie()
                {
                    Name = movieNode.Attributes["Name"].Value,
                    Director = movieNode.Attributes["Director"].Value,
                    MainPlayer = movieNode.Attributes["MainPlayer"].Value,
                    Price = decimal.Parse(movieNode.Attributes["Price"].Value),
                    ImgPath = movieNode.Attributes["ImgPath"].Value,
                    Description = movieNode.Attributes["Description"].Value,
                };
                Movies.Add(movie);

                //解析电影场次
                foreach (XmlNode timeNode in movieNode.ChildNodes)
                {
                    movie.MovieTimes.Add(new MovieTime()
                    {
                        Time = timeNode.Attributes["Time"].Value,
                        Tickets = CreateTickets(8, 8)
                    });
                }
            }
        }

        // 生成座位
        public List<Ticket> CreateTickets(int col, int row)
        {
            List<Ticket> tickets = new List<Ticket>();
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    tickets.Add(new Ticket()
                    {
                        TicketName = $"{1 + i}-{1 + j}"
                    });
                }
            }

            return tickets;
        }

        // 查看电影票
        public List<Ticket> GetTicketsByTime(string movieName, string time)
        {
            var movie = Movies.FirstOrDefault(m => m.Name == movieName);
            return movie.MovieTimes.FirstOrDefault(m => m.Time == time).Tickets;
        }

        // 买票
        public decimal[] BuyTickets(string movieName, string time, List<string> ticketNames, MovieTicketTypeEnums MovieTicketType = MovieTicketTypeEnums.全票, double discount = 0.8)
        {
            var movie = Movies.FirstOrDefault(m => m.Name == movieName);
            var price = movie.Price;
            var tickets = movie.MovieTimes.FirstOrDefault(m => m.Time == time).Tickets.Where(m => ticketNames.Contains(m.TicketName));
            if (MovieTicketType == MovieTicketTypeEnums.全票)
                discount = 1;
            else if (MovieTicketType == MovieTicketTypeEnums.赠票)
                discount = 0;
            if (tickets.LongCount() == 0)
                return new decimal[] { price * (decimal)discount, (decimal)0 };
            decimal sumPrice = 0;
            price = price * (decimal)discount;
            foreach (var ticket in tickets)
            {
                if (ticket.IsSaled) throw new Exception("该票已售出!");
                ticket.IsSaled = true;
                //price = price * (decimal)discount;
                sumPrice += price;
                //switch (MovieTicketType)
                //{
                //    case MovieTicketTypeEnums.学生票:
                //        price = price * (decimal)discount;
                //        sumPrice += price;
                //        break;
                //        ;

                //    case MovieTicketTypeEnums.赠票:
                //        price = 0;
                //        sumPrice += price;
                //        break;

                //    case MovieTicketTypeEnums.全票:
                //        sumPrice += price;
                //        break;
                //        ;

                //    default:
                //        throw new ArgumentOutOfRangeException(nameof(MovieTicketType), MovieTicketType, null);
                //}
            }

            return new decimal[] { price, sumPrice };
        }
    }
}